<?php
include("../common.inc.php");
InitGP(array("page","action","bid","oid","delids")); //初始化变量全局返回
include("function_common.php");
//初始化对象
include(INC_PATH."/order.class.php");
$o=new OrderClass();

if(empty($action)){
	InitGP(array("money_state","gid","order_sn","uid")); //初始化变量全局返回
	if($money_state == 'no')$wherestr[]="money_state=0";//未收款
	if($money_state == 'yes')$wherestr[]="money_state=2";//已收款
	if(!empty($gid))$wherestr[]="gid='{$gid}'";
	if(!empty($uid))$wherestr[]="uid={$uid}";
	if(!empty($order_sn))$wherestr[]="order_sn like '%{$order_sn}%'";
	if(!empty($wherestr)) $wheresql = implode(' AND ', $wherestr);	//条件汇总
	//获取当前页码
	$total=$o->getcount($wheresql); 							  //总信息数
	$pagesize=15;												  //一页显示信息数
	$page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量
	$offset=($page-1)*$pagesize;   								  //偏移量
	$dataarray=$o->getdata("$offset,$pagesize",$wheresql); //获取团购数据
	
	//包含后台模板文件
	include("tpl/order_list.htm");
}elseif ($action=="send") {
	InitGP(array("oid","gid","freight","consignee","fname","address","tel","zip","uid","uname","delivery_sn","delivery_sn","order_sn","Submit","tmp")); //初始化变量全局返回
	if ($Submit) {
		
		$addarray=array(
			"oid"=>GetNum($oid),
			"order_sn"=>$order_sn,
			"delivery_sn"=>$delivery_sn,
			"uid"=>GetNum($uid),
			"uname"=>$uname,
			"freight"=>GetNum($freight),
			"delivery_fee"=>0,
			"consignee"=>$consignee,
			"fname"=>$fname,
			"address"=>$address,
			"tel"=>$tel,
			"zip"=>$zip,
			"tmp"=>$tmp
		);
		
		include(INC_PATH."/invoice.class.php");
		$inv=new InvoiceClass();
		$info=$inv->add($addarray);
		if ($info="OK") {
			showmsg("發貨單填寫成功！","order_list.php");
		}else {
			showmsg($info,"-1");
		}
		
	}else {
		$value=$o->getone($oid);
		
		include("tpl/order_send.htm");
	}
	
}elseif ($action=="money") {
	InitGP(array("oid"));
	include_once(INC_PATH."/member.class.php");
    $m=new memberclass();
	    	
	$value=$o->getone($oid);
	$usermoney=$m->moneyedit($value['uname']);
	if ($usermoney>0) {
		$note="訂單：".$value["order_sn"]."管理員收款";
		$priceCount=$value['order_total_price']-$value['order_incharge'];
		if ($priceCount<=0) {
			showmsg("訂單金額錯誤","-1");
		}
		if ($usermoney>=$priceCount) {
			
			$m->moneyedit($value['uname'],- $priceCount,$note);
			$o->edit_money_state($oid,2,$priceCount,"購物金");
			showmsg("收款成功！","order_list.php");
		}else{
			$m->moneyedit($value['uname'],- $usermoney,$note);//账户余额减去
			$o->edit_money_state($oid,1,$usermoney,"購物金");
			showmsg("部分收款成功！","order_list.php");
		}
	}else showmsg("用戶帳戶餘額為空，收款失敗!","-1");

	
}elseif ($action=="edit") {
	InitGP(array("bid","gid","sn","password","endtime","usetime","uid","Submit")); //初始化变量全局返回
	if(!empty($Submit)){
		//处理提交动作
		if(isset($gid) && isset($bid)){
		//处理数据
		$gid=GetNum($gid);
		$uid=GetNum($uid);
		$sn=Char_cv($sn);
		$password=Char_cv($password);
		$endtime=strtotime($endtime);
		$endtime=strtotime($usetime);
		$arrayedit=array(
		"gid"=>$gid,
		"uid"=>$uid,
		"sn"=>$sn,
		"password"=>$password,
		"endtime"=>$endtime,
		"usetime"=>$usetime
		);
		//执行编辑操作
		$info=$o->edit($bid,$arrayedit);
		if($info=="OK"){
			showmsg("編輯團購卷成功!","groupbond_list.php");//成功
		}else showmsg($info,"groupbond_add.php");//出错！
	}else showmsg("缺少参数","groupbond_add.php");//出错！
		
	}else {
	
	//编辑团购信息表单
	$dataedit=$o->getdata(1,"bid={$bid}");
	$value=$dataedit[0];//获取第一条记录
	//包含后台模板文件
	include("tpl/groupbond_edit.htm");	
	}
}

?>

